<?php 
   
   /**
   * 数据库操作的单列类
   */
   class Db {
		
	static private $_instance;
	//注：类的成员变量可以不加var，如果加了var则不能用private等关键字修饰
	//php中代码结束的分号不能省略
    private  $dbConfig = ['host'=>'localhost','user'=>'luojie', 'password'=>'123', 'databass'=>'appInterface'];
    private  $_connectSource;
	 function __construct(){
       
  	}
    static function shareInstance () {
    	//instanceof检查某个实例是不是这个类，或者有没有实现某个接口，或者是不是某个类的子类
      if (!(self::$_instance instanceof self)) {
      	self::$_instance=new self();
      }
      return self::$_instance;
    }

    public function connect () {
      if (!$this -> _connectSource ) {
      	$host = $this->dbConfig['host'];
      	$user = $this->dbConfig['user'];
        $password = $this->dbConfig['password'];
        $databass = $this->dbConfig['databass'];

      	$this->_connectSource = mysql_connect($host, $user, $password);
      	if (!$this -> _connectSource) {
      		throw new Exception("数据库连接失败".mysql_error(), 1);
      		// die('数据库连接失败'.mysql_error());
      	}
        mysql_select_db($databass,$this->_connectSource);
        mysql_query('set names UTF8');
        var_dump($this->_connectSource);
      }
      return $this -> _connectSource;
    }


   }
   try {
   	$connectSource = Db::shareInstance()->connect()
   } catch (Exception $e) {
   	echo "数据库连接失败".$e->getMessage();
   }
  









?>